package com.liang.serializable;

import java.io.*;
import java.util.ArrayList;

public class ManyObject {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Student stu1 = new Student("zhangsan", 23);
        Student stu2 = new Student("lisi", 24);
        Student stu3 = new Student("wamgwu", 25);

        /*method1(stu1, stu2, stu3);

        method2();*/

//        method3(stu1, stu2, stu3);

//        method4();
//        method5(stu1);

        method6();

    }

    public static void method6() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day_13\\a.txt"));
        Object o = ois.readObject();
        System.out.println(o);
    }

    public static void method5(Student stu1) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day_13\\a.txt"));
        oos.writeObject(stu1);
        oos.close();
    }


    public static void method4() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day_13\\a.txt"));
        ArrayList<Student> o = (ArrayList<Student>)ois.readObject();
        System.out.println(o);
    }

    public static void method3(Student stu1, Student stu2, Student stu3) throws IOException {
        ArrayList<Student> list = new ArrayList<>();
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day_13\\a.txt"));
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
        oos.writeObject(list);
    }

    public static void method2() throws IOException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day_13\\a.txt"));
        while (true) {
            try {
                Student o = (Student)ois.readObject();
                System.out.println(o);

            } catch (EOFException e) {
                break;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        ois.close();
    }

    public static void method1(Student stu1, Student stu2, Student stu3) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day_13\\a.txt"));
        oos.writeObject(stu1);
        oos.writeObject(stu2);
        oos.writeObject(stu3);

        oos.close();
    }
}
